﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;

namespace SketchUpAPI.NET.Objects
{
    /// <summary>
    /// 所有引用类型的基类
    /// </summary>
    /// <remarks>对应 SketchUpAPI 中的 SUEntityRef</remarks>
    public class Entity : ObjectBase
    {
        #region API 函数
        /// <summary>
        /// 获取类型
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUType SUEntityGetType(IntPtr entity);

        /// <summary>
        /// 获取模型ID
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUEntityGetID(IntPtr entity, out int id);

        /// <summary>
        /// 模型永久ID
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="entity_pid"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUEntityGetPersistentID(IntPtr entity, out int entity_pid);

        /// <summary>
        /// 获取该物件所属物件表
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="entities"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUEntityGetParentEntities(IntPtr entity, out IntPtr entities);

        /// <summary>
        /// 获取所属的SketchUp模型
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUEntityGetModel(IntPtr entity, out IntPtr model);
        #endregion

        /// <summary>
        /// 类型
        /// </summary>
        public SUType Type
        {
            get
            {
                return SUEntityGetType(Id);
            }
        }

        /// <summary>
        /// 模型ID
        /// </summary>
        public int ID
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUEntityGetID(Id, out int id));
                return id;
            }
        }

        /// <summary>
        /// 永久ID
        /// </summary>
        public int PersistentID
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUEntityGetPersistentID(Id, out int pid));
                return pid;
            }
        }

        /// <summary>
        /// 模型所属的物件表
        /// </summary>
        public EntityTable Parent
        {
            get
            {
                SUEntityGetParentEntities(Id, out IntPtr id);
                return new EntityTable(id);
            }
        }

        /// <summary>
        /// 所属SketchUp模型
        /// </summary>
        public SketchUpModel Model
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUEntityGetModel(Id, out IntPtr id));
                return new SketchUpModel(id);
            }
        }

        internal Entity() { }

        internal Entity(IntPtr id) => Id = id;
    }
}